package com.stars.controller.system;
import com.stars.entity.system.SysJsItemEntity;
import com.stars.services.system.SysJsItemAccessServices;
import com.stars.services.system.SysJsItemConfigServices;
import com.stars.entity.dto.system.SysJsItemDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.common.annotation.Log;
import com.stars.common.base.BaseMsg;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.controller.BaseController;
import com.stars.common.enums.OperationType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;

import com.stars.common.valid.UpdateView;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @Title: 前端组件 mybatis dao
 * @author dj
 * @date 2019-10-14 20:33:38
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/sysJsItemCtl")
@Api(tags ="前端组件")
public class SysJsItemCtl extends BaseController{
   @Autowired
   private SysJsItemAccessServices  sysJsItemAccessServicesImpl;
   @Autowired
   private SysJsItemConfigServices  sysJsItemConfigServicesImpl;

   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "前端组件"+BaseMsg.MODULAR_PAGE,notes ="前端组件"+ BaseMsg.MODULAR_PAGE)
   @Log(detail = "前端组件"+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<SysJsItemDTO> pageSysJsItemList(Integer pageNum, Integer pageSize, SysJsItemDTO dto,HttpServletRequest request) throws Exception{
        return sysJsItemAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }

    /**
    * 保存
    * @param path
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "前端组件"+ BaseMsg.MODULAR_ADD,notes = "前端组件"+BaseMsg.MODULAR_ADD)
    @Log(detail = "前端组件"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addSysJsItem(String path) throws Exception{
        return sysJsItemConfigServicesImpl.addItem(path);
    }
    /**
    * 更新
    * @param updateEntityDto
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = "前端组件"+ BaseMsg.MODULAR_UPDATE,notes = "前端组件"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "前端组件"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg updateSysJsItem(@Validated({UpdateView.class}) SysJsItemEntity updateEntityDto,HttpServletRequest request) throws Exception{
        SysJsItemEntity entity = sysJsItemAccessServicesImpl.selectById(updateEntityDto.getId());
        ObjectCopyUtils.copyPropertiesIf(updateEntityDto,entity, BaseConstant.UPDATE);
        return sysJsItemConfigServicesImpl.updateSelectiveById(entity);
    }

    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "前端组件"+ BaseMsg.MODULAR_FIND_ID,notes = "前端组件"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = "前端组件"+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findSysJsItemById(String id) throws Exception{
        SysJsItemEntity entityName = sysJsItemAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }

     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "前端组件"+BaseMsg.MODULAR_DEL,notes ="前端组件"+BaseMsg.MODULAR_DEL)
     @Log(detail ="前端组件"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     public ResultMsg delSysJsItemById(String ids) throws Exception{
         return sysJsItemConfigServicesImpl.deleteById(ids);
     }
    /**
     * 删除所有
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/delAll")
    @ApiOperation(value = "前端组件"+BaseMsg.MODULAR_DEL,notes ="前端组件"+BaseMsg.MODULAR_DEL)
    @Log(detail ="前端组件"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
    public ResultMsg delAll() throws Exception{
        List<SysJsItemEntity> sysJsItemEntityList = sysJsItemAccessServicesImpl.selectListAll();
        for (SysJsItemEntity sysJsItemEntity : sysJsItemEntityList) {
            sysJsItemConfigServicesImpl.delete(sysJsItemEntity);
        }
        return ResultMsg.success();
    }

}